//#include <stdio.h>
#include "treenode.h"
#include "semantic.h"
#include "translate.h"
#include "targetcode.h"
extern FILE *yyin;
extern int yylineno;
extern treenode* root;
extern int unhandle;
bool err = 0;
int main(int argc, char **argv)
{
    if (argc <= 1)
        return 1;
    FILE *f = fopen(argv[1], "r");
    if (!f)
    {
        perror(argv[1]);
        return 1;
    }
    yyrestart(f);
    yylineno = 1;
    yyparse();
    if (unhandle > 0)
    {
        fprintf(stderr,"Syntax error.\n");
    }
    if (!err)
    {
        err = 0;
        //printTree(0, root); // lab1
        semanticAnalysis(root); // lab2
        if(!err)
        {
            FILE* fout = fopen(argv[2], "w");
            if (!fout) {
                perror(argv[2]);
                return 1;
            }
            //translate(fout, root); //lab3
            interCode* codes = getTsCode(root);
            targetCode(fout, codes);
        }
    }
    return 0;
}